package com.wyvoid.forum.web.action.admin.usergroup;

import com.wyvoid.forum.web.action.admin.BaseAdminAction;
import com.wyvoid.forum.service.IUserGroupService;
import com.wyvoid.forum.domain.UserGroup;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.List;

import org.apache.commons.lang.StringUtils;

/**
 * Created by IntelliJ IDEA.
 * User: wangchao
 * Date: 2007-7-4
 * Time: 21:54:18
 * To change this template use File | Settings | File Templates.
 */
@UrlBinding(value = "/admin/normalUserGroup.do")
public class MemberUserGroupAction extends BaseAdminAction {
    long[] userGroupId;
    long groupId;


    String[] permissionsStr;
    private UserGroup userGroup;
    @SpringBean
    private IUserGroupService userGroupService;
    private List<UserGroup> memberUserGroups;


    public String[] getPermissionsStr() {
        return permissionsStr;
    }

    public void setPermissionsStr(String[] permissionsStr) {
        this.permissionsStr = permissionsStr;
    }

    public long getGroupId() {
        return groupId;
    }

    public void setGroupId(long groupId) {
        this.groupId = groupId;
    }

    public void setUserGroupService(IUserGroupService userGroupService) {
        this.userGroupService = userGroupService;
    }


    public long[] getUserGroupId() {
        return userGroupId;
    }

    public void setUserGroupId(long[] userGroupId) {
        this.userGroupId = userGroupId;
    }

    public UserGroup getUserGroup() {
        return userGroup;
    }

    public void setUserGroup(UserGroup userGroup) {
        this.userGroup = userGroup;
    }

    public List<UserGroup> getMemberUserGroups() {
        return memberUserGroups;
    }

    public void setMemberUserGroups(List<UserGroup> memberUserGroups) {
        this.memberUserGroups = memberUserGroups;
    }

    @DefaultHandler
    public Resolution normalUserGroupForm() {

        for (int i = 0; i < userGroupId.length; i++) {
            long groupId;
            groupId = userGroupId[i];


            if (groupId > 0) {
                String deleteId=context.getRequest().getParameter("delete["+groupId+"]");
                if(!StringUtils.isBlank(deleteId)){
                    UserGroup deleteUserGroup=userGroupService.getUserGroup(Long.parseLong(deleteId));
                    userGroupService.deleteUserGroup(deleteUserGroup);
                }else{
                String groupTitle = context.getRequest().getParameter("groupnew[" + groupId + "][grouptitle]");
                String pointHignerStr = context.getRequest().getParameter("groupnew[" + groupId + "][pointHigner]");
                String pointLowerStr=context.getRequest().getParameter("groupnew["+groupId+"][pointLower]");
                String starsStr = context.getRequest().getParameter("groupnew[" + groupId + "][stars]");
                int pointHigner = 0;
                int pointLower=0;
                int stars = 0;
                if (!StringUtils.isBlank(pointHignerStr)) {
                    pointHigner = Integer.parseInt(pointHignerStr);
                }
                if(!StringUtils.isBlank(pointLowerStr)){
                    pointLower=Integer.parseInt(pointLowerStr);
                }
                if (!StringUtils.isBlank(starsStr)) {
                    stars = Integer.parseInt(starsStr);
                }
                userGroup = userGroupService.getUserGroupById(groupId);
                userGroup.setTitle(groupTitle);
                userGroup.setPointHigner(pointHigner);
                userGroup.setPointLower(pointLower);
                userGroup.setStars(stars);
                userGroupService.updateUserGroup(userGroup);
                }
            }

        }

        String[] groupTitles = context.getRequest().getParameterValues("groupnew[-1][grouptitle]");
        String[] pointHignerStrs = context.getRequest().getParameterValues("groupnew[-1][pointHigner]");
        String[] pointLowerStrs=context.getRequest().getParameterValues("groupnew[-1][pointLower]");
        String[] starsStrs = context.getRequest().getParameterValues("groupnew[-1][stars]");
        for (int j = 0; j < groupTitles.length; j++) {
            String groupTitle = groupTitles[j];
            String pointHignerStr = pointHignerStrs[j];
            String pointLowerStr=pointLowerStrs[j];
            String starsStr = starsStrs[j];
            int pointHigner = 0;
            int pointLower=0;
            int stars = 0;
            if (!StringUtils.isBlank(groupTitle)) {
                if (!StringUtils.isBlank(pointHignerStr)) {
                    pointHigner = Integer.parseInt(pointHignerStr);
                }
                if(!StringUtils.isBlank(pointLowerStr)){
                    pointLower=Integer.parseInt(pointLowerStr);
                }
                if (!StringUtils.isBlank(starsStr)) {
                    stars = Integer.parseInt(starsStr);
                }
                userGroup = new UserGroup();
                userGroup.setTitle(groupTitle);
                userGroup.setPointHigner(pointHigner);
                userGroup.setPointLower(pointLower);
                userGroup.setStars(stars);
                userGroupService.saveUserGroup(userGroup);
            }
        }
        return new RedirectResolution("/forum/admin/userGroupManage.do", false);
    }

     @DontValidate
    public Resolution setDetailPurviewForm() {
        userGroup = userGroupService.getUserGroup(groupId);
        return getAdminResolution("usergroup/detailPurview");
    }

     @DontValidate
     public Resolution setDetaiPurview() {
        String permissions = "|";
        String title = userGroup.getTitle();
        long id = userGroup.getId();
        for (int i = 0; i < permissionsStr.length; i++) {
            String str=context.getRequest().getParameter(permissionsStr[i]);
            if(!str.equals("0")){
            permissions+=str+"|";
            }
        }
        UserGroup updateUserGroup = userGroupService.getUserGroup(id);
        updateUserGroup.setTitle(title);
        updateUserGroup.setPermissions(permissions);
        userGroupService.updateUserGroup(updateUserGroup);
        return new RedirectResolution("/forum/admin/normalUserGroup.do?setDetailPurviewForm&groupId="+id, false);
    }
}
